package com.example.home

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.activityViewModels
import androidx.viewpager2.widget.ViewPager2.OnPageChangeCallback
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.LogUtils
import com.example.base.RouterPath
import com.example.common.base.BaseFragment
import com.example.common.entity.SimpleTypeEntity
import com.example.common.mvi.BaseMviFragment
import com.example.home.adapter.HomePageAdapter
import com.example.home.databinding.FragmentHomeBinding
import com.example.home.intent.HomeViewIntent
import com.example.home.state.HomeUiState
import com.example.home.vm.HomeViewModel
import com.google.android.material.tabs.TabLayoutMediator
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = RouterPath.PATH_FRAGMENT_HOME)
class HomeFragment : BaseMviFragment<FragmentHomeBinding>() {
    private val mViewModel: HomeViewModel by activityViewModels()
    private lateinit var homePageAdapter:HomePageAdapter
    private val searchHotWords = listOf("视频","少年","青年","壮年")
    private val tabList = mutableListOf<SimpleTypeEntity>()
    private var fragments = mutableListOf<HomeListFragment>()

    override fun initViews() {
        //设置搜索热词

        mDataBinding.tvbSearch.setDatas(searchHotWords)
        mDataBinding.tvbSearch.setItemOnClickListener{hotKeyWords,_,->
            ARouter.getInstance().build(RouterPath.PATH_SEARCH_PAGE).withString("keyWords",hotKeyWords).navigation()
        }
        homePageAdapter = HomePageAdapter(fragments, this@HomeFragment.childFragmentManager, lifecycle)
        mDataBinding.vp2Home.offscreenPageLimit = 1
        mDataBinding.vp2Home.adapter = homePageAdapter

        //关联TabLayout与ViewPager2
        TabLayoutMediator(mDataBinding.tabTypeLayout, mDataBinding.vp2Home) { tab, position ->
            tab.text = tabList[position].typename
        }.attach()


        //设置VewPager2页面切换事件
        mDataBinding.vp2Home.registerOnPageChangeCallback(object : OnPageChangeCallback(){
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                //存在Fragment还没有绑定到Activity中现象
                if (fragments[position].isAdded){
                    fragments[position].lazyLoadData()
                } else {
                    mDataBinding.vp2Home.postDelayed({
                        fragments[position].lazyLoadData()
                    }, 350)
                }
            }
        })



    }

    override suspend fun sendViewIntent() {
        mViewModel.channel.send(HomeViewIntent.LoadVideoType)
    }

    override suspend fun collectUiState() {
        mViewModel.uiState.collect{
            when(it){
                is HomeUiState.LoadSimpleTypeSuccess-> initTabFragment(it.list)
                is HomeUiState.LoadSimpleTypeFail -> showToast(it.msg)
                else->{}
            }
        }
    }
    private fun initTabFragment(list: List<SimpleTypeEntity>){
        if(list.isNotEmpty()){
            tabList.add(SimpleTypeEntity("",0,0,-1,"","推荐"))
            fragments.add(HomeListFragment.newInstance(-1, ""))
            //添加分类数据
            list.forEach{
                tabList.add(it)
                fragments.add(HomeListFragment.newInstance(it.id,it.channelid))
            }

            //设置分类tab
            tabList.forEach{
                mDataBinding.tabTypeLayout.addTab(mDataBinding.tabTypeLayout.newTab().setText(it.typename))
            }

            homePageAdapter.notifyDataSetChanged()
        }
    }

    override fun getLayoutId(): Int =R.layout.fragment_home
}